package com.kj.xuyuan.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.InviteDetailsAdapter;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.InviteBean;
import com.kj.xuyuan.model.InviteDetailsBean;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的邀请详情
 */
public class InviteDetailsActivity extends BaseActivity {

    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.cl_details)
    ConstraintLayout clDetails;
    @BindView(R.id.tv_level_one)
    TextView tvLevelOne;
    @BindView(R.id.tv_level_two)
    TextView tvLevelTwo;
    @BindView(R.id.rv_invite)
    RecyclerView rvInvite;
    @BindView(R.id.sml_invite)
    SmartRefreshLayout smlInvite;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private List<InviteDetailsBean> data;
    private InviteDetailsAdapter adapter;
    private int type = 1;//1奖励排行榜 2我的邀请成绩
    private int page = 1;//分页,当type=2时有分页
    private String myJiangli;//我的分成奖励

    public static void actionStar(Context context,String myJiangli){
        Intent intent = new Intent(context,InviteDetailsActivity.class);
        intent.putExtra("myJiangli",myJiangli);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_invite_details;
    }

    @Override
    protected void initView() {
        setTitleText("我的邀请详情");

        myJiangli = getIntent().getStringExtra("myJiangli");
        tvMoney.setText(myJiangli);

        initRecyclerView();
        getData();
    }

    private void getData() {
        Map map = new HashMap();
        map.put("type",type+"");
        map.put("page",page+"");
        OkGoUtil.postRequest(getContext(), UrlConstants.MY_INVITE_DETAILS, map, new JsonCallback<BaseModel<List<InviteDetailsBean>>>() {
            @Override
            public void onSuccess(Response<BaseModel<List<InviteDetailsBean>>> response) {
                if (response != null && response.body() != null) {
                    if(page == 1){
                        data.clear();
                    }
                    data.addAll(response.body().getData());
                    adapter.notifyDataSetChanged();
                    llNoData.setVisibility(data.size() == 0 ? View.VISIBLE : View.GONE);
                }else{
                    ToastUtil.showToast(context,response.body().getMsg());
                }
                smlInvite.finishRefresh();
                smlInvite.finishLoadMore();
            }

            @Override
            public void onError(Response<BaseModel<List<InviteDetailsBean>>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
                smlInvite.finishRefresh();
                smlInvite.finishLoadMore();
            }
        });
    }

    private void initRecyclerView() {
        data = new ArrayList<>();
        adapter = new InviteDetailsAdapter();
        rvInvite.setLayoutManager(new LinearLayoutManager(context));
        rvInvite.setAdapter(adapter);
        adapter.setNewData(data);

        smlInvite.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
            }
        });

        smlInvite.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page ++;
                getData();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    @OnClick({R.id.tv_level_one, R.id.tv_level_two})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_level_one:
                if(type != 1){
                    type = 1;
                    page = 1;
                    getData();
                    tvLevelOne.setTextColor(getResources().getColor(R.color.color_333));
                    tvLevelTwo.setTextColor(getResources().getColor(R.color.gray9));
                }
                break;
            case R.id.tv_level_two:
                if(type != 2){
                    type = 2;
                    page = 1;
                    getData();
                    tvLevelOne.setTextColor(getResources().getColor(R.color.gray9));
                    tvLevelTwo.setTextColor(getResources().getColor(R.color.color_333));
                }
                break;
        }
    }
}